﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="IncomeBusinessCount.aspx.cs"
    Inherits="Thksoft.Module.ProjectDetails.IncomeBusinessCount" %>

<!DOCTYPE html>
<html class="L_UI">
<head id="Head1" runat="server">
    <title>营业收入计划总统计表</title>
    <link href="../../CSS/Main.css" rel="stylesheet" type="text/css" />
    <link href="../../UserCotrols/My97DatePicker/skin/WdatePicker.css" rel="stylesheet"
        type="text/css" />
    <script src="../../UserCotrols/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="../../Content/lib/jslinq/linq.js" type="text/javascript"></script>
    <script src="../../Scripts/correctpng.js" type="text/javascript"></script>
    <script src="../../Scripts/Total.js" type="text/javascript"></script>
    <script src="../../Scripts/LastSum.js" type="text/javascript"></script>
    <script src="../../Scripts/NowPlan.js" type="text/javascript"></script>
    <script src="../../Scripts/AddRow.js" type="text/javascript"></script>
    <style type="text/css">
        .txt {
            border: none;
            height: 18px;
            line-height: 18px;
            width: 100%;
        }

        td.text-left {
            text-align: left;
            padding-left: 5px;
        }
    </style>
    <script type="text/javascript">
        var app1 = {};
        //刷新前页面（前页面不许是通过layer插件交互的）
        app1.ReLoad = function () {
            top.$("#layui-layer" + top.layer.getFrameIndex(window.name))[0].ReLoad();
            //同时关闭页面
            var index = top.layer.getFrameIndex(window.name);
            top.layer.close(index);
        }
        app1.YanZheng_Jin_E = function (obj) {
            var reg = /^[-]?\d+[.]?\d*$/;
            var va = $(obj).val();
            if (!reg.test(va)) {
                alert("【温馨提示】输入格式不对！");
                $(obj).focus();
            }
        }
        app1.GetRequest = function (ifObj) {
            if (typeof ifObj == 'boolean') {
                if (ifObj) {
                    var obj = {};
                    obj.Data = this.GetRequest();
                    obj.QueryString = function (key) {
                        for (var item in obj.Data) {
                            if (item.toLocaleLowerCase() == key.toLocaleLowerCase()) {
                                return obj.Data[item];
                            }
                        }
                        return '';
                    }
                    return obj;
                }
            }
            var url = location.search; //获取url中"?"符后的字串
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        }
        app1.QueryString = app1.GetRequest(true).QueryString;
        app1.Init = function () {
            //绑定数据
            var dom = $("#table1 tbody")[0];
            //删除除了第一行的dom
            $(dom).children().each(function (index, item) {
                if (index != 0) {
                    $(item).remove();
                }
            })
            var v1 = 0, v2 = 0, v3 = 0;
            //加载数据
            for (var i = 0; i < app1.Data.Data.length; i++) {
                var current = app1.Data.Data[i];
                var trdom = $("<tr></tr>");
                trdom[0].entity = current;
                {
                    var tddom = $("<td></td>");
                    var input = $("<input type=\"text\" class=\"txt\" style=\"text-align: center;\" />");
                    input.val(current.Index);
                    tddom.append(input);
                    current.IndexDom = input;
                    current.GetIndex = function () {
                        return this.IndexDom.val();
                    }
                    trdom.append(tddom);
                }
                {
                    var tddom = $("<td style=\"padding-left:10px;\"></td>");
                    var input = $("<input type=\"text\" class=\"txt\" style=\"width:100%;text-align: left;\" />");
                    input.val(current.Name);
                    tddom.append(input);
                    trdom.append(tddom);
                    current.NameDom = input;
                    current.GetName = function () {
                        return this.NameDom.val();
                    }
                }
                {
                    var tddom = $("<td style=\"padding-right:10px;\"></td>");
                    var input = $("<input type=\"text\" class=\"txt\" style=\"text-align: right;\" />");
                    input.val(current.SaleNumber);
                    tddom.append(input);
                    trdom.append(tddom);
                    current.SaleNumberDom = input;
                    current.GetSaleNumber = function () {
                        return this.SaleNumberDom.val();
                    }
                    if (current.SaleNumber != "" && current.ParentIndex == "") {
                        v1 += parseFloat(current.SaleNumber);
                    }
                }
                {
                    var tddom = $("<td style=\"padding-right:10px;\"></td>");
                    var input = $("<input type=\"text\" class=\"txt\" style=\"text-align: right;\" />");
                    input.val(current.UnitePrice);
                    tddom.append(input);
                    trdom.append(tddom);
                    current.UnitePriceDom = input;
                    current.GetUnitePrice = function () {
                        return this.UnitePriceDom.val();
                    }
                    if (current.UnitePrice != "" && current.ParentIndex == "") {
                        v2 += parseFloat(current.UnitePrice);
                    }
                }
                {
                    var tddom = $("<td style=\"padding-right:10px;\"></td>");
                    var input = $("<input type=\"text\" class=\"txt\" style=\"text-align: right;\" />");
                    input.val(current.SumPrice);
                    tddom.append(input);
                    trdom.append(tddom);
                    current.SumPriceDom = input;
                    current.GetSumPrice = function () {
                        return this.SumPriceDom.val();
                    }
                    if (current.SumPrice != "" && current.ParentIndex == "") {
                        v3 += parseFloat(current.SumPrice);
                    }
                }
                {
                    var tddom = $("<td></td>");
                    var input = $("<input type=\"text\" class=\"txt\" style=\"text-indent:10px;width:100%;text-align: left;\" />");
                    input.val(current.Remark);
                    tddom.append(input);
                    trdom.append(tddom);
                    current.RemarkDom = input;
                    current.GetRemark = function () {
                        return this.RemarkDom.val();
                    }
                }
                {
                    var tddom = $("<td></td>");
                    var input = $("<input type=\"text\" class=\"txt\" style=\"text-align: center;\" />");
                    input.val(current.ParentIndex);
                    tddom.append(input);
                    trdom.append(tddom);
                    current.ParentIndexDom = input;
                    current.GetParentIndex = function () {
                        return this.ParentIndexDom.val();
                    }
                }
                $(dom).append(trdom);
            }
            console.log(v3);
            $("#hj1").html(v1.toFixed(0).toString());
            $("#hj2").html(v2.toFixed(2).toString());
            $("#hj3").html(v3.toFixed(2).toString());
        }
        app1.Data = {};
        app1.Load = function () {
            var appId = this.QueryString("ApplyId");
            var state = this.QueryString("State");
            app1.Data.ApplyId = parseInt(appId);
            app1.Data.State = parseInt(state);
            app1.Data.Data = [];

            this.Service.Get(app1.Data.ApplyId, app1.Data.State, function (result) {
                if (result.Type == 1) {
                    var data = result.Data;
                    $(data).each(function (index, item) {
                        var entity = {};
                        entity.entity = item;
                        entity.Index = item.C_INDEX == null ? "" : item.C_INDEX.toString();
                        entity.Name = item.C_NAME = null ? "" : item.C_NAME;
                        entity.SaleNumber = item.SALENUMBER == null ? "" : item.SALENUMBER.toString();
                        entity.UnitePrice = item.UNITEPRICE == null ? "" : item.UNITEPRICE.toString();
                        entity.SumPrice = item.SUMPRICE == null ? "" : parseFloat(item.SUMPRICE).toFixed(2).toString();
                        entity.Remark = item.REMARK == null ? "" : item.REMARK;
                        entity.ParentIndex = item.PARENTINDEX == null ? "" : item.PARENTINDEX.toString();
                        app1.Data.Data.push(entity);
                    });
                    app1.Data.Data = Enumerable.From(app1.Data.Data).OrderBy("a=>a.Index").ToArray();
                    app1.Init();
                }
            })
        };
        //求和事件
        app1.SumClick = function () {
            for (var i = 0; i < app1.Data.Data.length; i++) {
                var current = app1.Data.Data[i];
                if (current.GetParentIndex() == "" && $.trim(current.GetIndex()) != "") {
                    var sum = 0;
                    for (var k = 0; k < app1.Data.Data.length; k++) {
                        var current1 = app1.Data.Data[k];
                        if ($.trim(current1.GetParentIndex()) == $.trim(current.GetIndex())) {
                            if ($.trim(current1.GetSumPrice()) != "") {
                                sum += parseFloat($.trim(current1.GetSumPrice()));
                            }
                        }
                    }
                    current.SumPriceDom.val(sum.toFixed(2).toString());
                }
            }
        };
        //提交事件
        app1.SubClick = function () {
            app1.SumClick();
            //app1.Data.Data

            var dt = [];
            for (var i = 0; i < app1.Data.Data.length; i++) {
                var current = app1.Data.Data[i];
                var entity = {};
                entity.Index = current.GetIndex();
                entity.Name = current.GetName();
                entity.SaleNumber = current.GetSaleNumber();
                entity.UnitePrice = current.GetUnitePrice();
                entity.SumPrice = current.GetSumPrice();
                entity.Remark = current.GetRemark();
                entity.ParentIndex = current.GetParentIndex();
                //验证数据
                if ($.trim(entity.Index) == "") {
                    alert("第" + (i + 1) + "行没有填写序号！");
                    return;
                }
                if ($.trim(entity.Name) == "") {
                    alert("第" + (i + 1) + "行没有填写项目名称！");
                    return;
                }
                if ($.trim(entity.SaleNumber) == "") {
                    entity.SaleNumber = "";
                }
                if ($.trim(entity.UnitePrice) == "") {
                    entity.UnitePrice = "";
                }
                if ($.trim(entity.ParentIndex) == "") {
                    entity.ParentIndex = "";
                }
                if ($.trim(entity.SumPrice) == "") {
                    if (entity.ParentIndex != "") {
                        alert("第" + (i + 1) + "行没有填写金额！");
                        return;
                    }
                    entity.SumPrice = 0;
                }
                dt.push(entity);
            }
            app1.Service.Update(app1.Data.ApplyId, app1.Data.State, dt, function (result) {
                if (result.Type == 1) {
                    alert("保存成功");
                    //提交成功重新加载一次页面
                    app1.ReLoad();
                    app1.Load();
                }
            });
        };
        //添加一行
        app1.InsertClick = function () {
            //console.log(app1.Data.Data);
            //console.log(app1.Data.Data[0].GetIndex());
            var dom = $("#table1 tbody");
            //先取出所有数据
            var current = {};
            current.entity = null;
            current.Index = (app1.Data.Data.length + 1).toString();
            current.Name = "";
            current.SaleNumber = "";
            current.UnitePrice = "";
            current.SumPrice = "";
            current.Remark = "";
            current.ParentIndex = "";
            app1.Data.Data.push(current);
            var trdom = $("<tr></tr>");
            trdom[0].entity = current;
            {
                var tddom = $("<td></td>");
                var input = $("<input type=\"text\" class=\"txt\" style=\"text-align: center;\" />");
                input.val(current.Index);
                tddom.append(input);
                current.IndexDom = input;
                current.GetIndex = function () {
                    return this.IndexDom.val();
                }
                trdom.append(tddom);
            }
            {
                var tddom = $("<td style=\"padding-left:10px;\"></td>");
                var input = $("<input type=\"text\" class=\"txt\" style=\"width:100%;text-align: left;\" />");
                input.val(current.Name);
                tddom.append(input);
                trdom.append(tddom);
                current.NameDom = input;
                current.GetName = function () {
                    return this.NameDom.val();
                }
            }
            {
                var tddom = $("<td style=\"padding-right:10px;\"></td>");
                var input = $("<input type=\"text\" class=\"txt\" style=\"text-align: right;\" />");
                input.val(current.SaleNumber);
                tddom.append(input);
                trdom.append(tddom);
                current.SaleNumberDom = input;
                current.GetSaleNumber = function () {
                    return this.SaleNumberDom.val();
                }
            }
            {
                var tddom = $("<td style=\"padding-right:10px;\"></td>");
                var input = $("<input type=\"text\" class=\"txt\" style=\"text-align: right;\" />");
                input.val(current.UnitePrice);
                tddom.append(input);
                trdom.append(tddom);
                current.UnitePriceDom = input;
                current.GetUnitePrice = function () {
                    return this.UnitePriceDom.val();
                }
            }
            {
                var tddom = $("<td style=\"padding-right:10px;\"></td>");
                var input = $("<input type=\"text\" class=\"txt\" style=\"text-align: right;\" />");
                input.val(current.SumPrice);
                tddom.append(input);
                trdom.append(tddom);
                current.SumPriceDom = input;
                current.GetSumPrice = function () {
                    return this.SumPriceDom.val();
                }
            }
            {
                var tddom = $("<td></td>");
                var input = $("<input type=\"text\" class=\"txt\" style=\"text-indent:10px;width:100%;text-align: left;\" />");
                input.val(current.Remark);
                tddom.append(input);
                trdom.append(tddom);
                current.RemarkDom = input;
                current.GetRemark = function () {
                    return this.RemarkDom.val();
                }
            }
            {
                var tddom = $("<td></td>");
                var input = $("<input type=\"text\" class=\"txt\" style=\"text-align: center;\" />");
                input.val(current.ParentIndex);
                tddom.append(input);
                trdom.append(tddom);
                current.ParentIndexDom = input;
                current.GetParentIndex = function () {
                    return this.ParentIndexDom.val();
                }
            }
            $(dom).append(trdom);
        };
        //删除一行
        app1.RemoveClick = function () {
            var index = -1;
            if ($.trim($("#p_id").val()) != "") {
                index = parseInt($("#p_id").val()) - 1;
            }
            if (index == -1) {
                index = app1.Data.Data.length - 1;
                if (index == -1) {
                    return;
                }
            }
            console.log(index);
            //先取出所有数据
            for (var i = 0; i < app1.Data.Data.length; i++) {
                if (i == index) {
                    $("#table1 tr:eq(" + (i + 1).toString() + ")").remove();
                    app1.Data.Data.splice(index, 1);
                    for (var k = 0; k < app1.Data.Data.length; k++) {
                        var current = app1.Data.Data[k];
                        current.Index = (k + 1).toString();
                        current.IndexDom.val(current.Index);
                    }
                    return;
                }
            }
        };
        app1.SqlHelper = {
            Query: function (sql, callback) {
                $.post("/Funds/PlanWeekFunds1.aspx?Ajax=1", JSON.stringify({
                    Code: "Query",
                    Data: {
                        Sql: sql
                    }
                }), function (data) {
                    if (data.Type == 1) {
                        callback(data.Data);
                    }
                })
            },
            NoQuery: function (sql, callback) {
                $.post("/Funds/PlanWeekFunds1.aspx?Ajax=1", JSON.stringify({
                    Code: "NoQuery",
                    Data: {
                        Sql: sql
                    }
                }), function (data) {
                    if (data.Type == 1) {
                        callback(data.Data);
                    }
                })
            }
        };
        app1.Service = {
            Get: function (applyId, applyState, callback) {
                $.post("/Module/ProjectDetails/IncomeBusiness1.aspx?Ajax=1", JSON.stringify({
                    Code: "Get",
                    Data: {
                        ApplyId: applyId,
                        ApplyState: applyState
                    }
                }), callback)
            },
            Update: function (applyId, applyState, dt, callback) {
                $.post("/Module/ProjectDetails/IncomeBusiness1.aspx?Ajax=1", JSON.stringify({
                    Code: "Update",
                    Data: {
                        ApplyId: applyId,
                        ApplyState: applyState,
                        Data: dt
                    }
                }), callback)
            }
        };
        $(function () {
            app1.Load();
        });
    </script>
</head>
<body class="bgcss">
    <form id="form1" runat="server">
        <div class="Window">
            <div class="Content" style="top: 0px;">
                <div class="Header" style="bottom: 35px; height: inherit; overflow: inherit; overflow-y: auto;">
                    <table style="width: 100%;">
                        <tr>
                            <td>
                                <table id="table1" class="cont_list" style="width: 100%; border-collapse: collapse;"
                                    cellspacing="0" border="1">
                                    <tbody>
                                        <asp:GridView ID="grvShow1" runat="server" AutoGenerateColumns="False" CssClass="cont_list"
                                            Width="100%" OnRowDataBound="grvShow_RowDataBound" EmptyDataText="没有查找到您要的数据"
                                            AllowSorting="True" OnSorting="grvShow1_Sorting" Visible="true">
                                            <Columns>
                                                <asp:BoundField DataField="C_INDEX" HeaderText="序号" SortExpression="C_INDEX" ItemStyle-Width="2%" />
                                                <asp:BoundField DataField="C_NAME" HeaderText="项目名称" SortExpression="C_NAME" ItemStyle-Width="20%" />
                                                <asp:BoundField DataField="SaleNumber" HeaderText="销售量" SortExpression="SaleNumber"
                                                    ItemStyle-Width="8%" />
                                                <asp:BoundField DataField="UnitePrice" HeaderText="单价" SortExpression="UnitePrice"
                                                    ItemStyle-Width="8%" />
                                                <asp:BoundField DataField="SumPrice" HeaderText="金额" SortExpression="SumPrice" ItemStyle-Width="8%" />
                                                <asp:BoundField DataField="Flow" HeaderText="银行现金流向" SortExpression="Flow" ItemStyle-Width="10%" />
                                                <asp:BoundField DataField="Remark" HeaderText="备注" SortExpression="Remark" />
                                                <asp:BoundField DataField="ParentIndex" HeaderText="上一级序号" SortExpression="ParentIndex"
                                                    ItemStyle-Width="5%" />
                                            </Columns>
                                        </asp:GridView>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="Content" style="top: auto; bottom: 0px; height: 35px; overflow: inherit; text-align: center;">
                </div>
            </div>
        </div>
    </form>
</body>
</html>